package org.lq.er.view.announcement;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import org.lq.er.entity.Announcement;
import org.lq.er.service.impl.AnnouncementServiceImpl;

/**
 * 管理员公告视图层
 * @author  张鹤山
 *
 */
public class Admin_announcement extends JFrame implements ActionListener, MouseListener{

	private JPanel contentPane;
	private JTable table;
	private DefaultTableModel dtm;
	private JScrollPane scrollPane;
	private PopupMenu popupMenu1;
	private AnnouncementServiceImpl as;
	private Admin_announcement admin = this;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Admin_announcement frame = new Admin_announcement();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Admin_announcement() {
		setTitle("管理公告");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 1000, 540);
		setLocationRelativeTo(null);
		contentPane = new JPanel(null);
		contentPane.setBackground(Color.white);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		JLabel label = new JLabel("公 告 管 理");
		label.setFont(new Font("宋体", Font.BOLD, 22));
		label.setBounds(430, 10, 140, 57);
		contentPane.add(label, BorderLayout.NORTH);

		Object[] row = new Object[]{"序号", "内容" ,"发布时间","最后修改时间"};
		dtm = new DefaultTableModel(row, 0);
		table = new JTable(dtm) {
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		JTableHeader tableHeader = table.getTableHeader(); 
		table.setRowHeight(30);
		tableHeader.setReorderingAllowed(false); //不可调整表头位置
		tableHeader.setResizingAllowed(false); //不可调整表头大小
		as = new AnnouncementServiceImpl();
		List<Announcement> list = as.findAll();
		for(Announcement a : list)
		{
			row[0] = a.getId();
			row[1] = a.getInformation();
			row[2] = a.getRelease_time();
			row[3] = a.getAlter_time();
			dtm.addRow(row);
		}
		table.addMouseListener(this);

		scrollPane = new JScrollPane(table);
		scrollPane.setBounds(50, 80, 900, 350);
		contentPane.add(scrollPane);

		JButton button = new JButton("添加");
		button.setBounds(864, 44, 73, 23);
		contentPane.add(button);
		button.addActionListener(this);

		popupMenu1 = new PopupMenu();
		MenuItem menuItem1 = new MenuItem("update");
		MenuItem menuItem2 = new MenuItem("delete");
		menuItem1.addActionListener(new ActionListener() { 

			@Override
			public void actionPerformed(ActionEvent e) {
				int a = JOptionPane.showConfirmDialog(Admin_announcement.this, "你确定要修改该公告么", "确定", JOptionPane.OK_CANCEL_OPTION);
				int index = (int)table.getValueAt(table.getSelectedRow(), 0);
				if(a == 0) {
					if(index > -1) {
						String information = (String)table.getValueAt(table.getSelectedRow(), 1);
						dtm.removeRow(table.getSelectedRow());
						new AnnouncementAdd(admin, information, index).setVisible(true);
					}
					else {
						JOptionPane.showMessageDialog(Admin_announcement.this, "请选择修改的对象", "错误", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
		});
		menuItem2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				int a = JOptionPane.showConfirmDialog(Admin_announcement.this, "你确定要删除该公告么", "确定", JOptionPane.OK_CANCEL_OPTION);
				if(a == 0) {
					if(as.delete((int)table.getValueAt(table.getSelectedRow(), 0))) {
						JOptionPane.showMessageDialog(Admin_announcement.this, "删除成功", "提示", JOptionPane.INFORMATION_MESSAGE);
						dtm.removeRow(table.getSelectedRow());
						dtm.fireTableStructureChanged();
					}else {
					}
				}
			}
		});
		popupMenu1.add(menuItem1);
		popupMenu1.add(menuItem2);
		contentPane.add(popupMenu1);
	}

	public void tableAdd() {
		DefaultTableModel newModel = (DefaultTableModel) table.getModel();
		Object[] obj = new Object[4];
		List<Announcement> list = as.findAll();
		obj[0] = list.get(0).getId();
		obj[1] = list.get(0).getInformation();
		obj[2] = list.get(0).getRelease_time();
		obj[3] = list.get(0).getAlter_time();
		newModel.addRow(obj);
		newModel.fireTableStructureChanged();// 更新显示
	}

	
	public void esc(Announcement a) {
		DefaultTableModel newModel = (DefaultTableModel) table.getModel();
		Object[] obj = new Object[4];
		obj[0] = a.getId();
		obj[1] = a.getInformation();
		obj[2] = a.getRelease_time();
		obj[3] = a.getAlter_time();
		newModel.addRow(obj);
		newModel.fireTableStructureChanged();// 更新显示
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
//		if(!AnnouncementAdd.open)
			new AnnouncementAdd(admin).setVisible(true);
	}

	/**
	 * 右键菜单
	 */
	@Override
	public void mouseClicked(MouseEvent e) {
		if(e.getButton() == 3) {
			if(table.getSelectedRow() > -1) {
				popupMenu1.show(table, e.getXOnScreen()-this.getX()-50, e.getYOnScreen()-this.getY()-110);
			}
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

}
